#!/bin/bash

# Requires https://github.com/mlaiacker/rosbag2video

# Usage: ~/recording_ws/src/record_social_dining/extract_all_bags_video.sh

# Before runnning, remove previously generated files with the same name!


input_dir=/home/emprise/recording_ws/src/record_social_dining/bag
# input_dir=/media/emprise/FCF4C425F4C3E04E/Janko-SocialDining_Backup/bag

output_dir=/home/emprise/recording_ws/src/record_social_dining/extracted_video
rosbag2video_script=/home/emprise/recording_ws/src/record_social_dining/rosbag2video/rosbag2video.py
cnt=0

echo "Input path: ${input_dir}"
echo "Output path: ${output_dir}"

for bag_filepath in ${input_dir}/*.bag
do

   cnt=$((cnt+1))
   session_id=$(basename -- $bag_filepath)
   session_id=${session_id:0:-4}
   start_time=$(date +%s)

   for participant_position in 0 1 2 3
   do
      ${rosbag2video_script} --fps 30 -t /camera${participant_position}/color/image_raw/compressed -o ${output_dir}/${session_id}_${participant_position}.mp4 ${bag_filepath}
   done

   end_time=$(($(date +%s)-start_time))
   echo "Session ID:" $session_id
   echo "Time taken (seconds): "$end_time
   echo ""
done

echo ""
echo "Extracted mp4 videos from ${cnt} bag files."
